﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AsteroidWar
{
    class StarField
    {
        private List<Sprite> stars = new List<Sprite>();
        private int screenWidth;
        private int screenHeight;
        private Random rand = new Random();
        
        //Color of the star
        private Color[] colors = { Color.White, Color.Wheat, Color.SlateGray, Color.Yellow, Color.WhiteSmoke };

        public StarField(
            int screenWidth, 
            int screenHeight, 
            int starCount, 
            Vector2 starVelocity, 
            Texture2D texture, 
            Rectangle frameRectanlge) 
        {
            this.screenHeight = screenHeight;
            this.screenWidth = screenWidth;

            for (int i = 0; i < starCount; i++)
            {
                stars.Add(new Sprite(
                    new Vector2(rand.Next(0, screenWidth),rand.Next(0, screenHeight)), 
                    texture, 
                    frameRectanlge, 
                    starVelocity));

                Color starColor = colors[rand.Next(0, colors.Count())];

                //Scale four component of color to adjust star's opacity
                starColor *= (float)(rand.Next(30, 90) / 100f); 
                stars[stars.Count() - 1].TintColor = starColor;
            }
        }

        public void Update(GameTime gameTime)
        {
            foreach (Sprite star in stars)
            {
                star.Update(gameTime);
                if (star.Location.Y > screenHeight)
                {
                    star.Location = new Vector2(rand.Next(0, screenWidth),0);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Sprite star in stars)
            {
                star.Draw(spriteBatch,1.0f);
            }
        }      
    }
}
